package com.erps.config.res;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.erps.CommonUtils.ActionResult;
import com.erps.CommonUtils.AppConfig;
import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.RequestUtils;
import com.erps.CommonUtils.Sequence;
import com.erps.CommonUtils.StringEx;
import com.erps.CommonUtils.ValueType;
import com.erps.dao.ErspResHumanDao;
import com.erps.dao.ErspResInfoDao;
import com.erps.dao.ErspUserFootprintsDao;
import com.erps.db.DBManager;
import com.erps.sys.rest.ErspCodeValueRest;
import com.erps.sys.rest.ErspResHumanRest;
import com.erps.sys.rest.PageData;
import com.erps.sys.rest.PageInfo;
import com.hmkj.entity.ErspResHuman;
import com.hmkj.entity.ErspUserFootprints;
import com.hmkj.entity.vo.ErspCodeValueVO;
import com.jfinal.core.Controller;

/**
 * DefaultController
 */
public class HumanController extends Controller {
	private static final Logger log = LoggerFactory.getLogger(HumanController.class);

	public void index() {
		int iPageSize = StringEx.getInt(this.getPara("_pagesize"), 20);
		int iPageNo = StringEx.getInt(this.getPara("_pageno"), 1);
		String cViewType = StringEx.getStr(getPara("showtype"));
		String cProZone = StringEx.getStr(getPara("prozone"));
		String cKeyWord = StringEx.getStr(getPara("keyword"));
		String cSort = StringEx.getStr(this.getPara("sort"));

		setAttr("showtype", cViewType);
		setAttr("prozone", cProZone);
		setAttr("keyword", cKeyWord);
		setAttr("sort", cKeyWord);

		ErspResHumanRest vUser = new ErspResHumanRest();
		PageInfo vPage = new PageInfo();
		vPage.setPagesize(iPageSize);
		vPage.setSort(cSort);

		PageData ret = vUser.Query(iPageNo, iPageSize, cSort, cViewType, cProZone, cKeyWord);
		vPage.setRowcount(ret.getRowcount());

		vPage.setPageno(iPageNo);

		setAttr("res_list", ret.getKeylist());

		// 类型库
		ErspCodeValueRest vDict = new ErspCodeValueRest();
		List<ErspCodeValueVO> vType1 = vDict.Find(43);
		List<ErspCodeValueVO> vType4 = vDict.Find(44);
		setAttr("typeid_43_list", vType1);
		setAttr("typeid_44_list", vType4);

		setAttr("page", vPage);
		render("ersp_res_human.ftl");
	}

	public void info() {
		String cKeyID = StringEx.getStr(this.getPara("id"));
		if (!StringEx.isEmpty(cKeyID)) {
			ErspUserFootprintsDao dao = new ErspUserFootprintsDao();
			ErspUserFootprints vo = new ErspUserFootprints();
			vo.setRes_id(cKeyID);
			vo.setUser_id(StringEx.getStr(this.getSessionAttr(AppConfig.SESSION_USERGUID)));
			dao.add(vo);
		}
		setAttr("info", new ErspResHumanRest().FindAsMap(cKeyID));
		render("info.ftl");
	}

	public void add() {
		ErspCodeValueRest vDict = new ErspCodeValueRest();
		List<ErspCodeValueVO> vType43 = vDict.Find(43);
		List<ErspCodeValueVO> vType44 = vDict.Find(44);
		List<ErspCodeValueVO> vType45 = vDict.Find(45);
		List<ErspCodeValueVO> vType46 = vDict.Find(46);

		setAttr("typeid_43_list", vType43);
		setAttr("typeid_44_list", vType44);
		setAttr("typeid_45_list", vType45);
		setAttr("typeid_46_list", vType46);

		render("rlzy.ftl");
	}

	public void save() {
		HttpServletRequest request = this.getRequest();
		ErspResInfoDao aMaster = new ErspResInfoDao();
		aMaster = (ErspResInfoDao) RequestUtils.getFromRequest(aMaster, request);
		aMaster.setDBTableName("ersp_res_info");
		String cKeyID = Sequence.getGuid();
		aMaster.setFieldValue("id", cKeyID);
		aMaster.setFieldValue("res_checked", "0");
		aMaster.setFieldValue("res_type", "1002");
		aMaster.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));
		aMaster.setFieldValue("create_time", String.valueOf(DateUtils.getDayTimeNum()));
		aMaster.setFieldValue("org_id", StringEx.getStr(getSessionAttr(AppConfig.SESSION_ORGID)));

		ErspResHumanDao aSlave = new ErspResHumanDao();
		aSlave = (ErspResHumanDao) RequestUtils.getFromRequest(aSlave, request);
		aSlave.setFieldValue("res_id", cKeyID);
		aSlave.setFieldValue("id", cKeyID);
		aSlave.setFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));

		String cImageList = StringEx.getStr(request.getParameter("img_id_list"));
		String[] ImageList = cImageList.split(",");
		String[] sqls = new String[2 + ImageList.length];
		sqls[0] = aMaster.getParmSQL();
		List<Object[]> ParmList = new ArrayList<Object[]>();
		ParmList.add(aMaster.getParmList());

		sqls[1] = aSlave.getParmSQL();
		ParmList.add(aSlave.getParmList());

		for (int i = 0; i < ImageList.length; i++) {
			sqls[2 + i] = "update ersp_res_picture set res_id='" + cKeyID + "' where id='" + ImageList[i] + "'";
			ParmList.add(null);
		}

		ActionResult vret = aMaster.Validate();
		ActionResult ret;
		if (vret == null)
			vret = aSlave.Validate();
		if (vret != null) {
			ret = vret;
		} else {

			int iCode = DBManager.ExecSQL(sqls, ParmList);
			if (iCode > 0)
				ret = ActionResult.Valid(ValueType.SUCCESS);
			else
				ret = ActionResult.Valid(ValueType.FAILURE);
		}
		renderJson(JSON.toJSONString(ret));
	}
}
